# ts文件爬取合成mp4
# 导入相关包或模块
from pathlib import Path
import json
import sys
import threading, queue
import time, os, subprocess
import requests, urllib, parsel
import random, re, base64
from tqdm import tqdm
from datetime import datetime


##开发中的问题：
# 参考地址：https://blog.csdn.net/sinat_39629323/article/details/106365843
# 1. 如何导入文本
# 2. 解析html方法
# 3.
# Python问题：
#
# 1. 如何下载视频到本地？
# 2. 如何ts合成mp4？
# 3. 如何遍历出ts列表并拼接完整？
# 4. 指定保存路径
# 5. xpath的用法
# 6. 合并的顺序问题
# 7. 下载的顺序问题
# 8. 删除视频问题
#
# 视频问题：
# 1. 如何获取m3u8真实url
# 2. 标题的中英文转换


# fwh-拿到播放页网址
def get_bofangye_url(url):
    # r = requests.get(url)
    # contents = r.text

    fpath = os.path.dirname(os.path.abspath('__file__'))
    html = sys.argv[2] if len(sys.argv) > 2 else fpath + "/dict/test01.txt"
    contents = Path(html).read_text()

    response = parsel.Selector(contents)

    # 获取英文的title
    titileEngList = []
    titleList = response.xpath('//span[@class="video-title title-truncate m-t-5"]/text()')
    for title in titleList:
        titileEngList.append(title.get())

    # 获取播放页的url列表
    urlList = response.xpath('//div[@class="well well-sm videos-text-align"]/a/@href')
    singleUrlList = []
    for single in urlList:
        singleUrl = single.get()
        # print(homePage)
        singleUrlList.append(singleUrl)

    # bofangye_url = urlList.get(1)
    # print(bofangye_url)
    return singleUrlList


# todo-fwh-需要解析出m3u8真实地址-很多是由js替换得到的
def get_m3u8_url(url):
    # r = requests.get(url)
    # html = r.text
    # print(html)

    fpath = os.path.dirname(os.path.abspath('__file__'))
    html = sys.argv[2] if len(sys.argv) > 2 else fpath + "/dict/test02.txt"
    contents = Path(html).read_text()
    print(contents)
    # a = re.findall("strencode2\('(.*?)\)", contents)
    a = re.findall("strencode2\(\"(.*?)\"\)", contents)
    print(a)
    # a = re.findall("base64decode\('(.*?)\)", contents)[0]
    temp_url = re.findall('\$(.*?)\#', urllib.parse.unquote(str(base64.b64decode(a))))
    print(temp_url)
    realUrl = temp_url
    return realUrl
    # response = parsel.Selector(contents)
    # urlList = response.xpath('//div[@class="well well-sm videos-text-align"]/a/@href')


# 拿到js文件网址
def get_js_url(bofangye_url):
    # r = requests.get(bofangye_url, headers=headers)
    r = requests.get(bofangye_url)
    response = parsel.Selector(r.text)
    js_url = 'https://www.dsm8.cc' + response.xpath('//div[@id="flash"]/script/@src').get()
    return js_url


# fwh-请求m3u8获取ts列表网址
def get_all_url(temp_url):
    # r = requests.get(js_url, headers=headers)
    # r = requests.get(js_url)
    # a = re.findall("base64decode\('(.*?)\)", r.text)[0]
    # temp_url = re.findall('\$(.*?)\#', urllib.parse.unquote(str(base64.b64decode(a))))
    # r = requests.get(temp_url[0], headers=headers)

    # temp_url = "https://ccn.killcovid2021.com//m3u8/501901/501901.m3u8?st=nYc6th7eucFxAulqMo02Lg&e=1627829806"
    temp_url = "https://fdc.91p49.com/m3u8/501407/501407.m3u8"
    r = requests.get(temp_url)
    contents = r.text
    print(contents)

    # fpath = os.path.dirname(os.path.abspath('__file__'))
    # html = sys.argv[2] if len(sys.argv) > 2 else fpath + "/dict/test03.txt"
    # contents = Path(html).read_text()
    # print(contents)

    file_line = contents.split("\n")  # 读取文件里的每一行
    all_url = []

    for index, line in enumerate(file_line):
        if "EXTINF" in line:
            unknow = False
            # 拼出ts片段的URL
            pd_url = temp_url.rsplit("/", 1)[0] + "/" + file_line[index + 1]
            all_url.append(pd_url)
            # print(pd_url)

    return all_url


# fwh-循环下载ts列表
def down_for_ts(all_url, path):
    for num, url in enumerate(all_url):
        # r = requests.get(url, headers=headers)
        print(url)
        r = requests.get(url)

        if r.status_code == 200:
            with open(path + "%s.ts" % num, "wb") as code:
                code.write(r.content)
                print("%s下载完成" % num)


# 下载ts文件
def download_ts(urlQueue):
    while True:
        try:
            # 不阻塞的读取队列数据
            url = urlQueue.get_nowait()
            n = int(url[-6:-3])
        except Exception as e:
            break
        # response = requests.get(url, stream=True, headers=headers)
        response = requests.get(url)
        ts_path = "./ts/%03d.ts" % n  # 注意这里的ts文件命名规则
        with open(ts_path, "wb+") as file:
            for chunk in response.iter_content(chunk_size=1024):
                if chunk:
                    file.write(chunk)
        print("%03d.ts OK..." % n)


# fwh-合成mp4文件
def get_video(path, title):
    files = os.listdir(path)
    for file in tqdm(files, desc="正在转换视频格式："):
        with open(path + file, 'rb') as f1:
            with open(path + title + ".mp4", 'ab') as f2:
                f2.write(f1.read())
    print("合成结束……")
    os.remove(path + file)
    print("删除ts……")


if __name__ == '__main__':
    homeUrl = 'https://f0601.workgreat11.live/v.php?category=rf&viewtype=basic&page=1'
    # 获取首页分页列表
    # bofangyeUrlList = get_bofangye_url(homeUrl)
    # 获取主页的m3u8
    # url='https://f0601.workgreat11.live/view_video.php?viewkey=ce817f2c415c79b39713&page=1&viewtype=basic&category=rf'
    # for bofangyeUrl in bofangyeUrlList:
    #     realUrl = get_m3u8_url(bofangyeUrl)

    # all_url = get_all_url("realUrl")
    # all_url = get_all_url(realUrl)
    # 下面开始循环下载所有剧集
    path = "/Users/fwh/temp/ts/"
    # down_for_ts(all_url, path)

    title = "temp" + str(datetime.today().date())
    get_video(path, title)
